package com.Ad;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.Tool.Tools;
import com.unity3d.player.R;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import java.util.Timer;
import java.util.TimerTask;

public class AdActivity extends Activity {
    public static Activity activity = null;
    private int _keepTime = 5000;
    private Handler _handler;
    private Runnable _runnable;

    public static Activity getActivity() {
        if (activity == null) {
            activity = UnityPlayer.currentActivity;
        }
        return activity;
    }

    private Button _skipButton;
    private TextView _timeText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adview);

        _skipButton = findViewById(R.id.SkipButton); //通过id找到对应button
        _skipButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BackMainActivity(false);
            }
        });
        _timeText = findViewById(R.id.TimeText);
        _timeText.setText("测试设置Text");
        _runnable = new Runnable() {
            @Override
            public void run() {
                // 在这里编写需要延迟执行的代码
                BackMainActivity(true);
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStart() {
        super.onStart();
        _keepTime = 5000;
        startTimer();
        _handler = new Handler(Looper.getMainLooper());
        _handler.postDelayed(_runnable, _keepTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopTimer();
        _handler.removeCallbacks(_runnable);
    }

    private Timer timer;

    public void startTimer() {
        // 定时任务延迟执行的时间，单位为毫秒
        long delay = 0;

        // 定时任务的间隔时间，单位为毫秒
        long interval = 1000;
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                // 定时任务要执行的逻辑
                // 在这里编写你需要定时执行的代码
                _keepTime -= interval;
                UpdateUI();
            }
        };
        // 启动定时器
        timer.schedule(timerTask, delay, interval);
    }

    public void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    private void UpdateUI() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String text = String.valueOf(_keepTime);
                _timeText.setText(text);
            }
        });
    }

    private void BackMainActivity(Boolean success) {
        int code = success ? 1 : 0;
        Intent intent = new Intent(AdActivity.this, UnityPlayerActivity.class);
        startActivity(intent);
        startActivityForResult(intent, code);
        if (success) {
            String resultStr = String.valueOf(code);
            UnityPlayer.UnitySendMessage("Root", "OnShowAdCallBack", resultStr);
            Tools.ShowToast("成功", 3);
        } else {
            Tools.ShowToast("失败", 3);
        }
    }

}
